package models

import (
	"encoding/xml"
	"os"
	agent "yunzhan/rpc-server/pb"
)

type NodeResult struct {
	Node  *agent.NodeInfo
	Error error
}
type XmlConfiguration struct {
	XMLName    xml.Name      `xml:"configuration"`
	Properties []XmlProperty `xml:"property"`
}

type XmlProperty struct {
	Name        string `xml:"name"`
	Value       string `xml:"value"`
	Description string `xml:"description"`
}

func (c *XmlConfiguration) Save(filename string) error {
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	encoder := xml.NewEncoder(file)
	encoder.Indent("", "    ")
	if err := encoder.Encode(c); err != nil {
		return err
	}

	return nil
}

func (c *XmlConfiguration) SetProperty(name, value, comment string) {
	// 如果属性存在即更新
	for i := range c.Properties {
		if c.Properties[i].Name == name {
			c.Properties[i].Value = value
			c.Properties[i].Description = comment
			return
		}
	}
	c.Properties = append(c.Properties, XmlProperty{Name: name, Value: value, Description: comment})
}
